余烬缀记

IPv6 防火墙配置

edited on:

2024 年 1 月 23 日备注:

放弃该方案,使用上级路由器来配置,这样可以保护下面所有的设备,上级路由器的系统是 OpenWrt,因此配置允许特定端口入站还是比较简单的

编辑防火墙配置文件

vim /etc/config/firewall

在末尾添加下面的配置

config rule
	option name 'Allow-IPv6-Test-Inbound'
	option src 'wan'
	option proto 'tcp'
	option dest 'lan'
	option dest_port '<PORT>'
	option family 'ipv6'
	option target 'ACCEPT'

如果要匹配特定的设备可以再添加一行匹配设备的 MAC 地址的配置

	option dest_mac '00:00:00:00:00'

然后重启

/etc/init.d/firewall restart

# 废弃方案

最近网络需要关闭防火墙和 IPv6 Session 保护暴露在公网上,为确保一些敏感的服务不被暴力破解因此使用 nftables 阻止掉相关的流量

首先通过 SSH 连接到目标机器,再按个人喜欢在任意位置新建一个 nftables.nft文件

赋予该文件可执行权限

sudo chmod +x nftables.nft

编写内容

#!/usr/sbin/nft -f

# 如果想本规则集会在开机时执行,在 /etc/rc.local 文件添加该文件路径
# nft list ruleset > /etc/nftables.conf
# 重载规则集
# sudo systemctl reload nftables

# 清除当前规则集
table inet firewall
delete table inet firewall

table inet firewall {

    set exposure_ports {
        type inet_service
        elements = { 21, 22, 23, 53, 80, 443 }
    }

    chain input {
        type filter hook input priority 0; policy accept;

        # IPv4 规则,由于没有公网 IPv4 因此仅对 IPv6 做出限制
        ip saddr != { 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16 } tcp dport {80, 443} drop

        # IPv6 规则
        ip6 saddr != { fe80::/10 } tcp dport @exposure_ports drop
    }

}

执行该文件

sudo ./nftables.nft

查看 nft 规则集是否存在

sudo nft list ruleset